//
//  BigLetterView.m
//  <17.1> TypingTutor
//
//  Created by chen on 16/9/19.
//  Copyright © 2016年 tyrad. All rights reserved.
//

#import "BigLetterView.h"

@implementation BigLetterView


//将数据生成pdf非常容易, NSView有相关方法
- (IBAction)savePDF:(id)sender
{
    //NSData *data = [self dataWithPDFInsideRect:self.bounds];
    __block NSSavePanel *panel = [NSSavePanel  savePanel];
    
    [panel setAllowedFileTypes:@[@"pdf"]];
    
    [panel beginSheetModalForWindow:[self window] completionHandler:^(NSInteger result) {
        
        if (result == NSOKButton) {
            
            NSRect r = [self bounds];
            NSData *data = [self dataWithPDFInsideRect:r];
            NSError *error ;
            
            BOOL successFul = [data writeToURL:[panel URL] options:0 error:&error];
            
            if (successFul == NO) {
                NSAlert *a = [NSAlert alertWithError:error];
                [a runModal];
            }
        }
    }];
}



/*******************************/

- (void)awakeFromNib{
    
    _myString = @"" ;
    [self prepareAttributes];
}

- (void)prepareAttributes{
    
    _attributes = @{}.mutableCopy ;
    
    _attributes[NSFontAttributeName] = [NSFont userFontOfSize:74];
    _attributes[NSForegroundColorAttributeName] = [NSColor redColor];
}

- (void)drawRect:(NSRect)dirtyRect {
    [super drawRect:self.bounds];
    
    [[NSColor greenColor] setFill];
    [NSBezierPath fillRect:dirtyRect];
    
    
    //绘制文字
    [self drawStringCenteredIn:self.bounds];
    
    
    //是否为窗口的first responder
    //绘制毛边
    if ([[self  window]firstResponder] == self && [NSGraphicsContext currentContextDrawingToScreen]) {
        
        [NSGraphicsContext saveGraphicsState];
        NSSetFocusRingStyle(NSFocusRingOnly);
        [NSBezierPath fillRect:self.bounds];
        [NSGraphicsContext restoreGraphicsState];
        
        //蓝色填充
        //[[NSColor redColor] set];
        //[NSBezierPath setDefaultLineWidth:4];
        //[NSBezierPath strokeRect:self.bounds];
    }
}


- (void)drawStringCenteredIn:(NSRect)r{
    
    NSSize strSize = [self.myString sizeWithAttributes:_attributes];
    
    NSPoint strOrigin ;
    strOrigin.x = r.origin.x + (r.size.width - strSize.width)/2;
    strOrigin.y = r.origin.y + (r.size.height - strSize.height)/2 ;
    
    [_myString drawAtPoint:strOrigin withAttributes:_attributes];
}






/*******************************/


- (void)setMyString:(NSString *)myString{
    _myString = myString ;
    NSLog(@"now myString is %@", myString);
    
    [self setNeedsDisplay:YES];
}


- (BOOL)becomeFirstResponder{
    [self setNeedsDisplay:YES];
    return YES ;
}

- (BOOL)resignFirstResponder{
//    [self setNeedsDisplay:YES];
    [self setKeyboardFocusRingNeedsDisplayInRect:self.bounds] ;
    return YES;
}


//重载，处理键盘事件
- (BOOL)acceptsFirstResponder{
    return YES ;
}

/*******************************/


- (void)keyDown:(NSEvent *)event{
    [self interpretKeyEvents:@[event]];
}


- (void)insertText:(NSString *)insertString{ //重载方法
    [self setMyString:insertString];
}


- (void)insertTab:(id)sender{
    [[self window]selectKeyViewFollowingView:self];//重载方法
}

- (void)insertBacktab:(id)sender{
    [[self window]selectKeyViewPrecedingView:self];//重载方法
}

- (void)deleteBackward:(id)sender{//重载方法 退格键盘
    [self setMyString:@""];
}


@end
















